﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KYKJ.Monitor.APIs
{
    /// <summary>
    /// 云台控制
    /// </summary>
    public static class PTZControl
    {
        private static Int32 m_userID = -1;
        public static int m_channel = -1;
        private static bool bAuto = false;

        public static int Init(string ip, int port, string user, string pwd, int channel)
        {
            if (ip == "" || port < 0 || user == "" || pwd == "")
            {
                return -1;
            }

            bool initSDK = CHCNetSDK.NET_DVR_Init();
            if (initSDK == false)
            {
                return -1;
            }

            m_channel = channel;
            CHCNetSDK.NET_DVR_DEVICEINFO_V30 deviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30();
            //登录设备 Login the device
            m_userID = CHCNetSDK.NET_DVR_Login_V30(ip, port, user, pwd, ref deviceInfo);
            if (m_userID < 0)
            {
                return -1; //return (int) CHCNetSDK.NET_DVR_GetLastError();
            }
            return 1;
        }

        public static void Close()
        {
            ////注销登录 Logout the device
            //if (m_lRealHandle >= 0)
            //{
            //    return;
            //}

            if (m_userID >= 0)
            {
                CHCNetSDK.NET_DVR_Logout(m_userID);
                CHCNetSDK.NET_DVR_Cleanup();
            }
        }

        #region 云台方向

        public static void PtzLeftStart(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.PAN_LEFT, 0, speed);
        }

        public static void PtzLeftEnd(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.PAN_LEFT, 1, speed);
        }

        public static void PtzUpStart(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.TILT_UP, 0, speed);
        }

        public static   void PtzUpEnd(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.TILT_UP, 1, speed);
        }

        public static  void PtzRightStart(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.PAN_RIGHT, 0, speed);
        }

        public static  void PtzRightEnd(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.PAN_RIGHT, 1, speed);
        }

        public static  void PtzDownStart(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.TILT_DOWN, 0, speed);
        }

        public static  void PtzDownEnd(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.TILT_DOWN, 1, speed);
        }

        public static  void PtzLeftDownStart(uint speed)
        {

            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.DOWN_LEFT, 0, speed);
        }

        public static  void PtzLeftDownEnd(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.DOWN_LEFT, 1, speed);
        }

        public static  void PtzLeftUpStart(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.UP_LEFT, 0, speed);
        }

        public static  void PtzLeftUpEnd(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.UP_LEFT, 1, speed);
        }

        public static  void PtzRightDownStart(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.DOWN_RIGHT, 0, speed);
        }

        public static  void PtzRightDownEnd(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.DOWN_RIGHT, 1, speed);
        }

        public static  void PtzRightUpStart(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.UP_RIGHT, 0, speed);
        }

        public static  void PtzRightUpEnd(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.UP_RIGHT, 1, speed);
        }

        public static  void PtzAuto(uint speed)
        {
            if (!bAuto)
            {
                CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.PAN_AUTO, 0, speed);
                bAuto = true;
            }
            else
            {
                CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.PAN_AUTO, 1, speed);
                bAuto = false;
            }
        }

        #endregion

        #region 焦点设置

        public static  void PtzZoomInStart(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.ZOOM_IN, 0, speed);
        }

        public static  void PtzZoomInEnd(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.ZOOM_IN, 1, speed);
        }

        public static  void PtzZoomOutStart(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.ZOOM_OUT, 0, speed);
        }

        public static  void PtzZoomOutEnd(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.ZOOM_OUT, 1, speed);
        }

        public static  void PtzFoucsNearStart(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.FOCUS_NEAR, 0, speed);
        }

        public static  void PtzFoucsNearEnd(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.FOCUS_NEAR, 1, speed);
        }

        public static  void PtzFoucsFarStart(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.FOCUS_FAR, 0, speed);
        }

        public static  void PtzFoucsFarEnd(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.FOCUS_FAR, 1, speed);
        }

        public static  void PtzIrisOpenStart(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.IRIS_OPEN, 0, speed);
        }

        public static  void PtzIrisOpenEnd(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.IRIS_OPEN, 1, speed);
        }

        public static  void PtzIrisCloseStart(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.IRIS_CLOSE, 0, speed);
        }

        public static  void PtzIrisCloseEnd(uint speed)
        {
            CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_userID, m_channel, CHCNetSDK.IRIS_CLOSE, 1, speed);
        }

        #endregion
    }
}
